package com.bawei.mall.product.controller;

import com.bawei.common.core.domain.R;
import com.bawei.common.core.web.controller.BaseController;
import com.bawei.common.security.utils.SecurityUtils;
import com.bawei.mall.product.domain.MallProductShoppingCart;
import com.bawei.mall.product.domain.model.ShoppingCartModel;
import com.bawei.mall.product.service.IMallProductShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName MallProductShoppingCartController
 * @Description 描述
 * @Author ZiZheng.Li
 * @Date 2024/7/5 16:53
 */
@RestController
@RequestMapping("/cart")
public class MallProductShoppingCartController extends BaseController {
    @Autowired
    private IMallProductShoppingCartService mallProductShoppingCartService;

    /**
     * 查询购物车信息列表
     */
    @GetMapping("/list")
    public R<List<ShoppingCartModel>> list()
    {
        List<ShoppingCartModel> list = mallProductShoppingCartService.queryList();
        return R.ok(list);
    }


    /**
     * 添加购物车
     */
    @PostMapping("/add")
    public R<MallProductShoppingCart> add(MallProductShoppingCart mallProductShoppingCart)
    {
        mallProductShoppingCart.setCusUserId(SecurityUtils.getUserId());
        //TODO 根据商品ID查询商品详情信息 获取到skuId
        mallProductShoppingCartService.save(mallProductShoppingCart);
        return R.ok();
    }

    /**
     * 删除购物车商品
     */
    @PostMapping("/delete")
    public R<MallProductShoppingCart> delete(String id)
    {
        mallProductShoppingCartService.removeById(id);
        return R.ok();
    }

    /**
     * 清空购物车
     */
    @PostMapping("/clear")
    public R<MallProductShoppingCart> clear()
    {
        mallProductShoppingCartService.removeById(SecurityUtils.getUserId());
        return R.ok();
    }
}
